﻿using System.Windows.Media;
using System.Windows.Shapes;
using Crusades.Helpers;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Crusades.Arrows;
namespace Crusades_UT.Arrows {
  [TestClass]
  public class Arrow_UT : PresentationTest {
    private Vector[] controlPoints;
    private Arrow arrow;

    [TestInitialize]
    public void Setup() {
      var P0 = new Vector(13, 159);
      var P1 = new Vector(26, 41);
      var P2 = new Vector(680, 272);
      var P3 = new Vector(575, 155);
      controlPoints = new[] { P0, P1, P2, P3 };

      arrow = new Arrow();
      TestPanel.Children.Add(arrow);
    }

    [TestMethod]
    public void AddsControl() {
      arrow.PercentFilled = .5;
      arrow.AddBezierSegment(controlPoints);
      arrow.RenderTo(.5);
      arrow.UpdateLayout();
      Assert.IsNotNull(arrow.FindName("Bezier0"));
    }

    [TestMethod]
    public void rendersAtPoint5(){
      arrow.PercentFilled = .5;
      
      arrow.AddBezierSegment(controlPoints);
      arrow.RenderTo(.5);
      arrow.UpdateLayout();
      Path graphic = (Path) arrow.FindName("Bezier0");
      
      Bezier bezier = new Bezier(controlPoints);
      Bezier splitBezier = bezier.SplitTo(.5);
      BezierSegment bSegment = getPathSegmentsFromPath(graphic)[0] as BezierSegment;
      Assert.AreEqual(bSegment.Point1, splitBezier.ControlPoints[1].toPoint());
      Assert.AreEqual(bSegment.Point2, splitBezier.ControlPoints[2].toPoint());
      Assert.AreEqual(bSegment.Point3, splitBezier.ControlPoints[3].toPoint());
      Assert.IsNotNull(bSegment);
      Assert.AreEqual(controlPoints[0].toPoint(), getFigures(graphic)[0].StartPoint);
    }

    [TestMethod]
    public void rendersAt1(){
      arrow.PercentFilled = 1;
      
      arrow.AddBezierSegment(controlPoints);
      arrow.RenderTo(1);
      arrow.UpdateLayout();
      Path graphic = (Path) arrow.FindName("Bezier0");
      
      Bezier bezier = new Bezier(controlPoints);
      BezierSegment bSegment = getPathSegmentsFromPath(graphic)[0] as BezierSegment;
      Assert.AreEqual(bSegment.Point1, bezier.ControlPoints[1].toPoint());
      Assert.AreEqual(bSegment.Point2, bezier.ControlPoints[2].toPoint());
      Assert.AreEqual(bSegment.Point3, bezier.ControlPoints[3].toPoint());
      Assert.IsNotNull(bSegment);
      Assert.AreEqual(controlPoints[0].toPoint(), getFigures(graphic)[0].StartPoint);
    }

    [TestMethod]
    public void AddBezierSegmentAddsArrowhead(){
      arrow.PercentFilled = .5;
      
      arrow.AddBezierSegment(controlPoints);
      arrow.RenderTo(.5);
      arrow.UpdateLayout();
      Path graphic = (Path) arrow.FindName("ArrowHead1");
      Assert.IsNotNull(graphic,"Arrowhead is missing");
    }

    private PathSegmentCollection getPathSegmentsFromPath(Path graphic) {
      PathGeometry geometry = (PathGeometry) graphic.Data;
      Assert.IsNotNull(geometry.Figures);
      Assert.IsInstanceOfType(geometry.Figures[0],typeof(PathFigure));
      PathFigure figure = (PathFigure) geometry.Figures[0];
      Assert.IsNotNull(figure.Segments);
      Assert.IsTrue(figure.Segments.Count>0);
      return figure.Segments;
    }

    private PathFigureCollection getFigures(Path graphic) {
      PathGeometry geometry = (PathGeometry) graphic.Data;
      Assert.IsNotNull(geometry.Figures);
      Assert.IsInstanceOfType(geometry.Figures[0],typeof(PathFigure));
      return geometry.Figures;
    }

  }
}
